package com.study.lxy.springcloud.common.format;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.study.lxy.springcloud.common.constance.CommonRespEnum;
import com.study.lxy.springcloud.common.exception.BusiException;

import java.util.List;

public class RespParse {
    public static void parseDummy(ServiceResp resp) throws BusiException {
        if(!CommonRespEnum.SUCCESS.getCode().equals(resp.getErrorCode())){
            throw new BusiException(resp.getErrorCode(),resp.getErrorMsg());
        }
    }

    public static <T> T parseObject(ServiceResp resp,Class<T> claz) throws BusiException{
        if(!CommonRespEnum.SUCCESS.getCode().equals(resp.getErrorCode())){
            throw new BusiException(resp.getErrorCode(),resp.getErrorMsg());
        }
        return JSON.parseObject(JSON.toJSONString(resp.getRespData()),claz);
    }

    public static <T> List<T> parseList(ServiceResp resp, Class<T> claz) throws BusiException{
        if(!CommonRespEnum.SUCCESS.getCode().equals(resp.getErrorCode())){
            throw new BusiException(resp.getErrorCode(),resp.getErrorMsg());
        }
        return JSONArray.parseArray(JSON.toJSONString(resp.getRespData()),claz);
    }
}
